#include "DateModel.h"


DateModel::DateModel()
{
	pool = ConnectionPool::getConnectPool();
	cout << "DateModel" << endl;
}

DateModel::~DateModel()
{

}

DateModel* DateModel::getDateModel()
{
	// 静态局部变量，保证只初始化一次
	static DateModel Model;
	return &Model;
}

shared_ptr<User> DateModel::getUser(string& username,string& phone) {
	shared_ptr<MysqlConn> conn = pool->getConnection();
	std::string sql = "SELECT * FROM User WHERE user_name = ? OR user_phone = ?";
	unique_ptr<PreparedStatement> pstmt(conn->getConn()->prepareStatement(sql));
	pstmt->setString(1, username);
	pstmt->setString(2, phone);
	unique_ptr<ResultSet> result(pstmt->executeQuery());
	std::shared_ptr<User> user = nullptr;
	if (result->next()) {
		user = std::make_shared<User>();  // 创建 shared_ptr 管理的 User 对象
		user->id = result->getInt("user_id");

		strncpy(user->username, result->getString("user_name").c_str(), sizeof(user->username) - 1);
		user->username[sizeof(user->username) - 1] = '\0';

		strncpy(user->imgpath, result->getString("user_headPath").c_str(), sizeof(user->imgpath) - 1);
		user->imgpath[sizeof(user->imgpath) - 1] = '\0';

		strncpy(user->userpass, result->getString("user_passwd").c_str(), sizeof(user->userpass) - 1);
		user->userpass[sizeof(user->userpass) - 1] = '\0';

		strncpy(user->phonenumber, result->getString("user_phone").c_str(), sizeof(user->phonenumber) - 1);
		user->phonenumber[sizeof(user->phonenumber) - 1] = '\0';
	}
	return user;    // 返回 shared_ptr，共享所有权
}

bool DateModel::insetUser(User& user)
{	
	shared_ptr<MysqlConn> conn = pool->getConnection();
	string sql = "INSERT INTO User(user_name, user_passwd, user_headPath, user_phone) VALUES (?, ?, ?,?)";
	unique_ptr<PreparedStatement> pstmt(conn->getConn()->prepareStatement(sql));
	// 设置参数，注意索引从 1 开始
	pstmt->setString(1, user.username);
	pstmt->setString(2, user.userpass);
	pstmt->setString(3, user.imgpath);
	pstmt->setString(4, user.phonenumber);
	int reslut = pstmt->executeUpdate();
	if (reslut>0)
	{
		cout << "已插入用户" << user.phonenumber << endl;
		return true;
	}
	return false;
}

bool DateModel::insetVideo(Video& video,int user_id)
{
	if (video.video_size==0)
	{
		cout << "无效视频" << endl;
		return false;
	}
	shared_ptr<MysqlConn> conn = pool->getConnection();
	string sql = "INSERT INTO Video(video_type, video_img, video_name, video_path, video_progress, video_size,user_id) VALUES(?, ?, ?, ?, ?, ?,?) ";
	unique_ptr<PreparedStatement> pstmt(conn->getConn()->prepareStatement(sql));
	// 设置参数，注意索引从 1 开始
	pstmt->setInt(1, video.video_type);
	pstmt->setString(2, video.video_img);
	pstmt->setString(3, video.video_name);
	pstmt->setString(4, video.video_path);
	pstmt->setInt(5, video.video_progress);
	pstmt->setInt(6, video.video_size);
	pstmt->setInt(7, user_id);
	int reslut = pstmt->executeUpdate();
	if (reslut > 0)
	{
		return true;
	}
	return false;
}

bool DateModel::insetImg(Img& img, int user_id,string &path)
{
	shared_ptr<MysqlConn> conn = pool->getConnection();
	string sql = "INSERT INTO Img(img_type,img_name,img_datapath, img_path,user_id) VALUES (?, ?,?,?,?) ";
	unique_ptr<PreparedStatement> pstmt(conn->getConn()->prepareStatement(sql));
	// 设置参数，注意索引从 1 开始
	pstmt->setInt(1, img.img_type);
	pstmt->setString(2, img.img_name);
	pstmt->setString(3, path);
	pstmt->setString(4, img.img_path);
	pstmt->setInt(5, user_id);
	int reslut = pstmt->executeUpdate();
	if (reslut > 0)
	{
		cout << "图片==" << img.img_path << endl;
		return true;
	}
	return false;
}

bool DateModel::updateVideo(int progress, int user_id,string &videoNmae)
{
	shared_ptr<MysqlConn> conn = pool->getConnection();
	string sql = "UPDATE Video SET video_progress = ? WHERE user_id=? and video_name=? ";
	unique_ptr<PreparedStatement> pstmt(conn->getConn()->prepareStatement(sql));
	pstmt->setInt(1, progress);
	pstmt->setInt(2, user_id);
	pstmt->setString(3, videoNmae);
	int reslut = pstmt->executeUpdate();
	if (reslut > 0)
	{
		return true;
	}
	return false;
}

bool DateModel::deleteVideo(int id, string name)
{
	shared_ptr<MysqlConn> conn = pool->getConnection();
	string sql = "DELETE FROM Video WHERE user_id=? and video_name=?";
	unique_ptr<PreparedStatement> pstmt(conn->getConn()->prepareStatement(sql));
	pstmt->setInt(1, id);
	pstmt->setString(2, name);
	int reslut = pstmt->executeUpdate();
	if (reslut > 0)
	{
		return true;
	}
	return false;
}


vector<Video> DateModel::video_list(int user_id,int num, int pagge)
{
	vector<Video> video_vector;
	shared_ptr<MysqlConn> conn = pool->getConnection();
	string sql = "SELECT video_img, video_name, video_path, video_type, video_progress, video_size FROM Video WHERE user_id = ? ORDER BY video_datetime DESC LIMIT ? OFFSET ?";
	unique_ptr<PreparedStatement> pstmt(conn->getConn()->prepareStatement(sql));
	pstmt->setInt(1, user_id);
	pstmt->setInt(2, num);
	pstmt->setInt(3, (pagge - 1) * num);
	unique_ptr<ResultSet> result(pstmt->executeQuery());
	while (result->next())
	{
		Video video;
		video.video_type = result->getInt("video_type");
		video.video_progress = result->getInt("video_progress");
		video.video_size=result->getInt("video_size");
		//防止溢出
		strncpy(video.video_name, result->getString("video_name").c_str(), sizeof(video.video_name) - 1);
		video.video_name[sizeof(video.video_name) - 1] = '\0';
		
		strncpy(video.video_img, result->getString("video_img").c_str(), sizeof(video.video_img) - 1);
		video.video_img[sizeof(video.video_img) - 1] = '\0';

		strncpy(video.video_path, result->getString("video_path").c_str(), sizeof(video.video_path) - 1);
		video.video_path[sizeof(video.video_path) - 1] = '\0';
		//加入容器
		video_vector.push_back(video);
	}
	return video_vector;
}

int DateModel::getVideoNum(int user_id)
{
	int num = 0;
	shared_ptr<MysqlConn> conn = pool->getConnection();
	string sql = "SELECT COUNT(video_id) as sum FROM Video WHERE user_id= ? ";
	unique_ptr<PreparedStatement> pstmt(conn->getConn()->prepareStatement(sql));
	pstmt->setInt(1, user_id);
	unique_ptr<ResultSet> result(pstmt->executeQuery());
	while (result->next())
	{
		num = result->getInt("sum");
	}
	return num;
}

int DateModel::getImgNum(int user_id)
{
	int num = 0;
	shared_ptr<MysqlConn> conn = pool->getConnection();
	string sql = "SELECT COUNT(img_id) as sum FROM Img WHERE user_id= ? ";
	unique_ptr<PreparedStatement> pstmt(conn->getConn()->prepareStatement(sql));
	pstmt->setInt(1, user_id);
	unique_ptr<ResultSet> result(pstmt->executeQuery());
	while (result->next())
	{
		num = result->getInt("sum");
	}
	return num;
}

vector<Img> DateModel::img_list(int user_id, int num, int pagge)
{
	vector<Img> img_vector;
	shared_ptr<MysqlConn> conn = pool->getConnection();
	string sql = "SELECT img_type, img_name, img_path, img_datetime FROM Img WHERE user_id = ? ORDER BY img_datetime DESC LIMIT ? OFFSET ?";
	unique_ptr<PreparedStatement> pstmt(conn->getConn()->prepareStatement(sql));
	pstmt->setInt(1, user_id);
	pstmt->setInt(2, num);
	pstmt->setInt(3, (pagge-1)*num);
	unique_ptr<ResultSet> result(pstmt->executeQuery());
	while (result->next())
	{
		Img img;
		img.img_type = result->getInt("img_type");
		//防止溢出
		strncpy(img.img_name, result->getString("img_name").c_str(), sizeof(img.img_name) - 1);
		img.img_name[sizeof(img.img_name) - 1] = '\0';

		strncpy(img.img_path, result->getString("img_path").c_str(), sizeof(img.img_path) - 1);
		img.img_path[sizeof(img.img_path) - 1] = '\0';

		strncpy(img.datetime, result->getString("img_datetime").c_str(), sizeof(img.datetime) - 1);
		img.datetime[sizeof(img.datetime) - 1] = '\0';
		//加入容器
		img_vector.push_back(img);
	}
	return img_vector;
}
